//
//  LoanViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/8/20.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class LoanViewController: KMViewController {
    
    fileprivate let scrollView = UIScrollView()
    fileprivate let headView = LoanHeadView() //头部视图
    fileprivate let quickLoansView = QuickLoansView() //快速贷款
    fileprivate let searchView = QuickLoanSearchView() //搜索框
    fileprivate let loanTypeView = LoanTypeView() //贷款类型
    fileprivate let genericLoanView = GenericLoansView() //最新资讯
    fileprivate let newLoanView = NewLoanView() //最新贷款攻略

    override func viewDidLoad() {
        super.viewDidLoad()

        addSubView()
    }
    
    //MARK: 添加子视图
    fileprivate func addSubView() {
        
//        headView.delegate = self
//        self.view.addSubview(headView)
    
        scrollView.backgroundColor = UIColor.mineColor()
        scrollView.showsVerticalScrollIndicator = false
        self.view.addSubview(scrollView)
    
        quickLoansView.delegate = self
        scrollView.addSubview(quickLoansView)
        
        searchView.searchButton.addTarget(self, action: #selector(searchLoanAmount), for: .touchUpInside)
        scrollView.addSubview(searchView)
        
        let adverteButton = UIButton()
        adverteButton.layer.contents = UIImage.init(named: "finan")?.cgImage
        adverteButton.addTarget(self, action: #selector(watchAdverte), for: .touchUpInside)
        scrollView.addSubview(adverteButton)
        
        loanTypeView.delegate = self
        scrollView.addSubview(loanTypeView)
        
        genericLoanView.delegate = self
        scrollView.addSubview(genericLoanView)
        
        newLoanView.delegate = self
        scrollView.addSubview(newLoanView)
        
//        headView.snp.makeConstraints { (make) in
//            make.top.left.right.equalToSuperview()
//            make.height.equalTo(RATIO_H(maxNum: 40))
//        }
        
        scrollView.snp.makeConstraints { (make) in
            make.left.right.top.bottom.equalToSuperview()
        }
        
        quickLoansView.snp.makeConstraints { (make) in
            make.left.right.equalTo(self.view)
            make.top.equalToSuperview()
            make.height.equalTo(ScreenW/1.11)
        }
        
        searchView.snp.makeConstraints { (make) in
            make.left.right.equalTo(self.view)
            make.top.equalTo(quickLoansView.snp.bottom)
            make.height.equalTo(RATIO_H(maxNum: 60))
        }
        
        adverteButton.snp.makeConstraints { (make) in
            make.left.right.equalTo(self.view)
        make.top.equalTo(searchView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.height.equalTo(RATIO_H(maxNum: 70))
        }
        
        loanTypeView.snp.makeConstraints { (make) in
            make.top.equalTo(adverteButton.snp.bottom)
            make.left.right.equalTo(self.view)
            make.height.equalTo(RATIO_H(maxNum: 300))
        }
        
        genericLoanView.snp.makeConstraints { (make) in
            make.top.equalTo(loanTypeView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.left.right.equalTo(self.view)
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        newLoanView.snp.makeConstraints { (make) in
            make.top.equalTo(genericLoanView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.left.right.equalTo(self.view)
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        KMTool.setRefresh(with: scrollView, headerAction: {[weak self] in
            
            self?.genericLoanView.getData()
            self?.newLoanView.getData()
            
        }, footerAction: nil)
        
        //获取贷款数据
        genericLoanView.getData()
        newLoanView.getData()
    }
    
    //MARK: 贷款金额搜索
    @objc fileprivate func searchLoanAmount() {
        
        if searchView.searchView.text == "" {
            KMTool.showText(_text: inputLoanAmount)
            return
        }
        
        var loanSum = "" //贷款金额

        if let sum = Double(searchView.searchView.text!) {
            
            if sum < 2 {
               loanSum = lessTwo
            }else if sum >= 2 && sum < 5 {
                loanSum = twoToFive
            }else if sum >= 5 && sum < 10 {
                loanSum = fiveToTen
            }else if sum >= 10 && sum < 50 {
                loanSum = tenToFifty
            }else if sum >= 50 && sum < 100 {
                loanSum = fiftyToHundred
            }else{
                loanSum = greateHundred
            }
        }else{
            loanSum = loanLimit
        }
        
        
        let genericLoanVC = GenericLoanViewController.init(loanSum: loanSum)
        genericLoanVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(genericLoanVC, animated: true)
    }
    
    //MARK: 查看广告
    @objc fileprivate func watchAdverte() {
        
        let loanDetailsWebVC = LoanDetailsWebViewController.init(urlStr: "4",title:PeaceBank + "-平安新一贷")
        loanDetailsWebVC.hidesBottomBarWhenPushed = true
        loanDetailsWebVC.removeElements = ["top-bar","footer"]
        self.navigationController?.pushViewController(loanDetailsWebVC, animated: true)
    }
    
    deinit {
        quickLoansView.clearTime()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
    

}

extension LoanViewController : LoanHeadViewDelegate, GenericLoansViewDelegate,NewLoanViewDelegate,LoanTypeViewDelegate,QuickLoansViewDelegate {
    
    //MARK: 快速贷款申请
    func quickLoanApplication() {
        
        let quickLoanVC = QuickLoansViewController()
        quickLoanVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(quickLoanVC, animated: true)
    }
    
    //MARK: 修改最新贷款攻略坐标
    func changeNewLoanViewFrame(count: Int) {
        
        let height = CGFloat(count) * RATIO_H(maxNum: 160) + RATIO_H(maxNum: 40)
        
        genericLoanView.snp.remakeConstraints { (make) in
            make.top.equalTo(loanTypeView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.left.right.equalTo(self.view)
            make.height.equalTo(height)
        }
        
        self.scrollView.mj_header.endRefreshing()
        
        self.view.layoutIfNeeded()
        scrollView.contentSize = .init(width: ScreenW, height: newLoanView.frame.maxY + RATIO_H(maxNum: 10))
    }
    
    //MARK: 修改最新贷款坐标
    func changeGenericLoansFrame(count: Int) {
        
        let height = CGFloat(count) * RATIO_H(maxNum: 50) + RATIO_H(maxNum: 40)
        newLoanView.snp.remakeConstraints { (make) in
            make.top.equalTo(genericLoanView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.left.right.equalTo(self.view)
            make.height.equalTo(height)
        }
        
        self.scrollView.mj_header.endRefreshing()
        
        self.view.layoutIfNeeded()
        scrollView.contentSize = .init(width: ScreenW, height: newLoanView.frame.maxY + RATIO_H(maxNum: 10))
        
    }
    
    //MARK: 查看最新资讯新闻
    func selectNews(model: NewLoanModel) {
        
        let href = webUrl + "/marticleloan/" + model.id!
        
        let webVC = WebViewController.init(urlString: href, title: model.title, shareImage: kmHttps + "/data/upload/" + model.image!, shareText: model.excerpt,shareTitle:model.title)
        webVC.removeElements = ["mblocks","crumbs mblock","app-download"] 
        webVC.removeIds = ["xiong_div","geturl"]
        webVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(webVC, animated: true)
    }
    
    //MARK: 查看贷款详情
    func selectLoanDetails(model: GenericLoansModel) {
        
        let loanDetailsWebVC = LoanDetailsWebViewController.init(urlStr: model.id!,title:model.organization_name! + "-" + model.product_name!)
        loanDetailsWebVC.hidesBottomBarWhenPushed = true
        loanDetailsWebVC.removeElements = ["top-bar","footer"]
        self.navigationController?.pushViewController(loanDetailsWebVC, animated: true)
        
    }
    
    //MARK: 选择职业类型
    func selectWorktype(index:Int,title: String) {
        
        var genericLoanVC : GenericLoanViewController!
        
        if index < 6 {
            genericLoanVC = GenericLoanViewController.init(workType: title)
            
        }else if index == 6 {
            genericLoanVC = GenericLoanViewController.init(mortgage: noCollateral)
        }else if index == 7 {
            genericLoanVC = GenericLoanViewController.init(mortgage: carCollateral)
        }
        
            genericLoanVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(genericLoanVC, animated: true)
    }
    
    //MARK: 选择贷款类型
    func selectLoanType(index: Int) {
        
        switch index {
        case 1:
            let genericLoanVC = GenericLoanViewController()
            genericLoanVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(genericLoanVC, animated: true)
        case 2:
            let houseCarLoanVC = HouseCarLoanViewController.init(title: houseLoan)
            houseCarLoanVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(houseCarLoanVC, animated: true)
        case 3:
            let houseCarLoanVC = HouseCarLoanViewController.init(title: carLoan)
            houseCarLoanVC.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(houseCarLoanVC, animated: true)
        case 4:
            let selectedLoanVC = SelectedLoanViewController()
            selectedLoanVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(selectedLoanVC, animated: true)
        default:
            break
        }
    }
    
    //MARK: 贷款精选
    func loanSelected() {
        let selectedLoanWebVC = SelectLoanWebViewController.init(urlStr: webUrl + "/mobile/choice/c95")
        selectedLoanWebVC.hidesBottomBarWhenPushed = true
        selectedLoanWebVC.removeElements = ["loan_header","loan_button","footer","app-download"]
        self.navigationController?.pushViewController(selectedLoanWebVC, animated: true)
    }
    
    //MARK: 更多贷款资讯
    func moreNewLoan() {
        
        let rootVC = UIApplication.shared.keyWindow?.rootViewController as? RootTabBarController
        rootVC?.selectedIndex = 2
        let nav = rootVC?.viewControllers![2] as? KMNavigationController
        let informationenVC = nav?.rt_visibleViewController as? InformationRootViewController
        informationenVC?.currentVC = 4
    }
    
    //MARK: 更多通用贷款
    func moreGenericLoan() {
       
        let genericLoanVC = GenericLoanViewController()
        genericLoanVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(genericLoanVC, animated: true)
    }
    
}
